var rksh = {
    initQuery: function () {
        //加载仓库
        $('#queryRksh_dhck').combobox({
            url: $.webPath() + '/organ/ck/ckmcList',
            valueField: 'id',
            textField: 'mc',
            panelHeight: '200',
            editable: true
        });
        $("[name='queryRksh.cggs']").jgbmtree($("[name='queryRksh.cgbm']"), $("[name='queryRksh.cgr']"), '', '');

        //客户加载
        $("[name='queryRksh.gys']").qyjgzcombobox();
    },
    initDatagrid: function () {
        $("#grid_rksh").datagrid(rksh.dataOptions);
    },
    initBtn: function () {
        /*$("#btn_rksh_ys").click(function () {
            rksh.showYsWindow({"rksh.id": $('#grid_rksh').datagrid('getSelected').id});
        });*/
        $("#btn_rksh_stock").click(function () {
            rksh.showStockWindow({"rksh.id": $('#grid_rksh').datagrid('getSelected').id});
        });
        $("#btn_rksh_cancel_ys").click(function () {
            var row = $('#grid_rksh').datagrid('getSelected');
            if (row.yrk != true && row.sfys == true) {
                $.messager.confirm('确认', '是否取消验收此采购单？', function (r) {
                    if (r) {
                        var index = $('#grid_rksh').datagrid('getRowIndex', row);
                        var newRow = row;
                        newRow["sfys"] = "否";
                        newRow["sfys_text"] = "未验收";
                        $.post($.webPath() + '/kcgl/rksh/cancel_rksh', {"rksh.id": row.id}, function (data) {
                            var obj = eval('(' + data + ')');
                            if (obj.statusCode == '200') {
                                $('#grid_rksh').datagrid('updateRow', {"index": index, row: newRow});
                                rksh.setBtnStatus();
//                                rksh.alwayshowDetail(row.id);
                                $.messager.show({title: '提示框', msg: obj.message, timeout: 3000, showType: 'slide'});
                            }
                            else {
                                $.messager.alert('提示', obj.message, 'info');
                            }
                        });
                    }
                });
            } else {
                $.messager.alert('提示', "请选择要取消验收的采购单！", 'info');
            }
        });
        $("#btn_rksh_bccgd").click(function () {
            var row = $('#grid_rksh').datagrid('getSelected');
            if (row.yrk == true) {
                $.messager.confirm('确认', '是否生成补充采购单？', function (r) {
                    if (r) {
                        $.post($.webPath() + '/cggl/cgdgl/bccgd', {"cgd.id": row.id}, function (data) {
                            var obj = eval('(' + data + ')');
                            if (obj.statusCode == '200') {
                                rksh.setBtnStatus();
//                                rksh.alwayshowDetail(row.id);
                                $.messager.show({title: '提示框', msg: obj.message, timeout: 3000, showType: 'slide'});
                            }
                            else {
                                $.messager.alert('提示', obj.message, 'info');
                            }
                        });
                    }
                });
            } else {
                $.messager.alert('提示', "请选择已经入库的入库单！", 'info');
            }
        });
        $("#btn_rksh_search").click(function () {
            $('#grid_rksh').datagrid({pageNumber: 1, queryParams: rksh.getQuery()});
        });
    },
    setBtnStatus: function () {
        var row = $("#grid_rksh").datagrid("getSelected");
        if (row && row.sfgq == "false") {
           // $("#btn_rksh_ys").hide();
           // $("#btn_rksh_cancel_ys").hide();
            $("#btn_rksh_stock").hide();
            $("#btn_rksh_bccgd").hide();
        } else {
            if (row && row.id && row.zt == "yfk" && row.yrk != true) {
               // $("#btn_rksh_ys").show();
               // $("#btn_rksh_cancel_ys").hide();
                $("#btn_rksh_stock").show();
                $("#btn_rksh_bccgd").hide();
            } else if (row && row.id && row.zt == "yfk" && row.yrk == true) {
               // $("#btn_rksh_ys").hide();
                //$("#btn_rksh_cancel_ys").show();
                $("#btn_rksh_stock").hide();
                $("#btn_rksh_bccgd").hide();
            } else {
                //$("#btn_rksh_ys").hide();
                //$("#btn_rksh_cancel_ys").hide();
                $("#btn_rksh_stock").hide();
                $("#btn_rksh_bccgd").show();
            }
        }
    },
    dataOptions: {
        url: $.webPath() + "/kcgl/rksh/rkshlist",
        pageNumber: 1,
        onDblClickCell: function (index, field) {
            if (field == "cgdh") {
                var row = $(this).datagrid("getSelected");
                if (row.zt == "ybc") rksh.showWindow({"rksh.id": row.id});
            }
        },
        loadFilter: function (data) {
            $(data.rows).each(function () {
                //this.sfys_text = this.sfys == true ? "已验收" : "未验收";
              this.zt_text = this.yrk == true ? "已入库" : "未入库";
                this.cglx_text = this.cglx == "cgrk" ? "采购入库" : this.cglx == "zddh" ? "直达到货" : this.cglx == "bght" ? "保供采购" : this.cglx == "nbcg" ? "内部采购" : "统配采购";
            })
            return data;
        },
        onLoadSuccess: function () {
            $(this).datagrid('selectRow', 0);
            rksh.setBtnStatus();
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onClickRow: function () {
            rksh.setBtnStatus();
        },
        onSelect: function (rowIndex, rowData) {
            rksh.setBtnStatus();
        },
        columns: [
            [
                {field: 'cgdh', title: '采购单号', align: 'center', width: 40, formatter: rkshCell},
                {field: 'fhrq', title: '发货日期', align: 'center', width: 16},
                {field: 'dhrq', title: '到货日期', align: 'center', width: 16},
                {field: 'gys_jc', title: '供应商名称', align: 'center', width: 36},
                {field: 'cglx_text', title: '采购类型', align: 'center', width: 13},
                {field: 'cggs_jc', title: '采购公司', align: 'center', width: 30},
                {field: 'sbgs_jc', title: '申报公司', align: 'center', width: 30},
                {field: 'sbr_xm', title: '申报人', align: 'center', width: 10},
              //  {field: 'sfys_text', title: '验收状态', align: 'center', width: 20},
                {field: 'zt_text', title: '状态', align: 'center', width: 15},
                {field: 'rksj', title: '入库日期', align: 'center', width: 16}
            ]
        ]
    },
   /* showYsWindow: function (cgdysdata) {
        rksh.win_rkys = $.window({
            title: "入库验收",
            iconCls: 'icon-add2',
            width: 1024,
            height: 500,
            url: $.webPath() + "/kcgl/rksh/rksh_ys",
            data: cgdysdata
        });
    },*/
    showStockWindow: function (cgddata) {
//        var buttons = [
//            {
//                text: '全部入库',
//                handler: rksh_stock
//            }
//      ]
        rksh.win_rksh = $.window({
            title: "入库审核",
            iconCls: 'icon-add2',
            width: 1024,
            height: 500,
            url: $.webPath() + "/kcgl/rksh/rksh_decideStock",
            data: cgddata
//            buttons:buttons
        });
    },
    win: {},
    showDetail: function (id) {
        if ($("#layout_rksh #rksh_id").val() != id) {
            rksh.alwayshowDetail(id);
        }
    },
    alwayshowDetail: function (id) {
        $('#layout_rksh').layout('remove', 'center');
        $('#layout_rksh').layout('add', {
            region: 'center',
            split: true,
            href: $.webPath() + '/kcgl/rksh/rksh_detail?rksh.id=' + id
        });
    },
    getQuery: function () {
        return {
            "yrk": $("[name='queryRksh.zt']").val(),
            "sfys": $("[name='queryRksh.sfys']").val(),
            "lx": $("[name='queryRksh.lx']").val(),
            "cggsId": $("#queryRksh_cggs").combotree('getValue'),
            "cgbmId": $("#queryRksh_cgbm").combobox('getValue'),
            "cgrId": $("#queryRksh_cgr").combobox('getValue'),
            "cgdh": $("[name='queryRksh.cgdh']").val(),
            "wzbm": $("[name='queryRksh.wzbm']").val(),
            "gysid": $("[name='queryRksh.gys']").val(),
            "dhck": $("#queryRksh_dhck").combobox('getValue'),
            "fhrqStart": $('#queryRksh_start').datebox('getValue'),
            "fhrqEnd": $('#queryRksh_end').datebox('getValue'),
            "sfgq": $("[name='queryRksh.sfgq']").val()
        }
    }
}

$(function () {
    //查询条件-加载
    rksh.initQuery();
    //按钮-加载
    rksh.initBtn();
    //列表-加载
    rksh.initDatagrid();

    setTimeout(function () {
        $("#btn_rksh_search").click();
    }, 500);

})
